﻿namespace worker_service.Tools
{
    /// <summary>
    /// 日志工具类
    /// </summary>
    public static class LogTool
    {
        /// <summary>
        /// 写信息日志
        /// </summary>
        /// <param name="text">日志内容文本</param>
        public static void LogInfo(string? text)
        {
            // 生成的日志文件相对路径: info_logs/202411/03.txt

            var now = DateTime.Now;

            string currentPath = AppContext.BaseDirectory;
            string folderPath = Path.Combine(currentPath, "info_logs", now.ToString("yyyyMM"));

            Directory.CreateDirectory(folderPath);

            string fileName = now.ToString("dd") + ".txt";
            string filePath = Path.Combine(folderPath, fileName);

            string formatText = $"[{now:HH:mm:ss} | INFO] => {text}\n";
            File.AppendAllText(filePath, formatText);
        }

        /// <summary>
        /// 写异常日志
        /// </summary>
        /// <param name="text">日志内容文本</param>
        public static void LogError(string? text)
        {
            // 生成的日志文件相对路径: error_logs/202411/03.txt

            var now = DateTime.Now;

            string currentPath = AppContext.BaseDirectory;
            string folderPath = Path.Combine(currentPath, "error_logs", now.ToString("yyyyMM"));

            Directory.CreateDirectory(folderPath);

            string fileName = now.ToString("dd") + ".txt";
            string filePath = Path.Combine(folderPath, fileName);

            string formatText = $"[{now:HH:mm:ss} | ERROR] => {text}\n";
            File.AppendAllText(filePath, formatText);
        }
    }
}
